package com.cskaoyan.shopping.controller;


import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductCateService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("shopping")
public class ShoppingController {

    @Autowired
    IProductCateService iProductCateService;

    @Autowired
    IProductService iProductService;

    @GetMapping("categories")
    public ResponseData<List<ProductCateDto>> categories() {
        String sort = "1";
        List<ProductCateDto> allProductCate = iProductCateService.getAllProductCate(sort);
        ResponseData<List<ProductCateDto>> data = new ResponseUtil<List<ProductCateDto>>().setData(allProductCate);
        return data;
    }

    //shopping/product/{id}
    @GetMapping("product/{id}")
    public ResponseData product(@PathVariable("id") Long id) {

        ProductDetailDto productDetail = iProductService.getProductDetail(id);
        return new ResponseUtil<ProductDetailDto>().setData(productDetail);
    }

    @GetMapping("goods")
    public ResponseData<AllProductResponse> list(@Validated PageRequest pageRequest) {
        AllProductResponse products = iProductService.getAllProduct(pageRequest);
        return new ResponseUtil<AllProductResponse>().setData(products);
    }
}
